package com.baking.controller;

import com.baking.model.Product;
import com.baking.service.ProductSerice;
import com.baking.util.FileUtil;
import com.baking.util.Msg;
import com.baking.util.RedPropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:wrb
 * @Date :2020/1/9/20:19
 * @Describtion:
 **/
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductSerice productSerice;

    @RequestMapping("/index")
    public String toProductPage(){
        return "/product/productPage";
    }

    @RequestMapping("/productEditPage")
    public String productEditPage(){
        return "/product/productEditPage";
    }

    /** @Description:
     * @Author:wrb
     * @Date:2020/1/11/21:25
     * @Param 
     * @return java.lang.String
     */
    @RequestMapping("/productDetailPage")
    public String productDetailPage(Product product){
        return "/product/productDetailPage";
    }

    @RequestMapping("/getProductList")
    @ResponseBody
    public Msg getProductList(@RequestParam(value = "productId",required = false) Integer productId,
                                 @RequestParam(value = "productName",required = false) String productName,
                                 @RequestParam (value="page",required = false) String page,
                                 @RequestParam (value="limit",required = false) String limit){
        Msg msg = new Msg();
        Product product = new Product();
        product.setProductName(productName);
        product.setProductId(productId);
        List<Product> productList = productSerice.getProductList(product);
        msg.setCode("0");
        msg.setData(productList);
        return msg;
    }

    @RequestMapping("/addProduct")
    @ResponseBody
    public Msg addProduct(Product product){
        Msg msg = new Msg();
        if(product.getData()!=null && product.getData().startsWith("data:image")){
            //保存图片并返回文件保存路径
            String fileSavePath = FileUtil.download(product.getData());
            if(fileSavePath!=null)
                product.setImg(fileSavePath);
        }
        boolean b = productSerice.addProduct(product);
        if (b) {
            msg.setMsg("新增成功");
        } else {
            msg.setMsg("新增失败");
        }
        return msg;
    }

    @RequestMapping("/deleteProduct")
    @ResponseBody
    public Msg deleteProduct(Product product){
        Msg msg = new Msg();
        boolean b = productSerice.deleteProduct(product);
        if (b) {
            msg.setMsg("删除成功");
        } else {
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @RequestMapping("/updateProduct")
    @ResponseBody
    public Msg updateProduct(Product product,HttpServletRequest request,HttpServletResponse response){
        Msg msg = new Msg();
        String d = product.getData();
        if(product.getData()!=null && product.getData().startsWith("data:image")){
            //保存图片并返回文件保存路径
            String fileSavePath = FileUtil.download(product.getData());
            if(fileSavePath!=null)
                product.setImg(fileSavePath);
        }
        boolean b = productSerice.updateProduct(product);
        if (b) {
            msg.setMsg("更新成功");
        } else {
            msg.setMsg("更新失败");
        }
        return msg;
    }

    @RequestMapping("/upImg")
    @ResponseBody
    public Msg upImg(){
        Msg msg = new Msg();
        msg.setCode("0");
        return msg;
    }

}
